package com.jungle.system.application.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jungle.common.annotation.Log;
import com.jungle.common.annotation.RepeatSubmit;
import com.jungle.common.core.controller.BaseController;
import com.jungle.common.core.domain.OptionVo;
import com.jungle.common.core.domain.PageQuery;
import com.jungle.common.core.domain.R;
import com.jungle.common.core.page.TableDataInfo;
import com.jungle.common.core.validate.AddGroup;
import com.jungle.common.core.validate.EditGroup;
import com.jungle.common.enums.BusinessType;
import com.jungle.common.utils.StringUtils;
import com.jungle.common.utils.poi.ExcelUtil;
import com.jungle.system.application.domain.bo.SysApplicationBo;
import com.jungle.system.application.domain.vo.SysApplicationVo;
import com.jungle.system.application.service.ISysApplicationService;
import com.jungle.system.domain.vo.SysOssVo;
import com.jungle.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 应用管理
 *
 * @author jungle
 * @date 2023-03-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/application/application")
public class SysApplicationController extends BaseController {

    private final ISysApplicationService iSysApplicationService;
    private final ISysOssService sysOssService;

    /**
     * 查询应用管理列表
     */
    @GetMapping("/protal/list")
    public TableDataInfo<SysApplicationVo> listD(SysApplicationBo bo, PageQuery pageQuery) {
        pageQuery.setOrderByColumn("orderNum");
        pageQuery.setIsAsc("desc");
        TableDataInfo<SysApplicationVo> sysApplicationVoTableDataInfo = iSysApplicationService.queryPageList(bo, pageQuery);

        for (SysApplicationVo row : sysApplicationVoTableDataInfo.getRows()) {
            SysOssVo byId = sysOssService.getById(Long.valueOf(row.getIcon()));
            if (byId != null) {
                row.setIcon(byId.getUrl());
            }
        }
        return sysApplicationVoTableDataInfo;
    }

    /**
     * 查询应用管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysApplicationVo> list(SysApplicationBo bo, PageQuery pageQuery) {
        pageQuery.setOrderByColumn("orderNum");
        pageQuery.setIsAsc("desc");
        return iSysApplicationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出应用管理列表
     */
    @SaCheckPermission("application:application:export")
    @Log(title = "应用管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysApplicationBo bo, HttpServletResponse response) {
        List<SysApplicationVo> list = iSysApplicationService.queryList(bo);
        ExcelUtil.exportExcel(list, "应用管理", SysApplicationVo.class, response);
    }

    /**
     * 获取应用管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("application:application:query")
    @GetMapping("/{id}")
    public R<SysApplicationVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable String id) {
        return R.ok(iSysApplicationService.queryById(id));
    }

    /**
     * 新增应用管理
     */
    @SaCheckPermission("application:application:add")
    @Log(title = "应用管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysApplicationBo bo) {
        return toAjax(iSysApplicationService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改应用管理
     */
    @SaCheckPermission("application:application:edit")
    @Log(title = "应用管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysApplicationBo bo) {
        return toAjax(iSysApplicationService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除应用管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("application:application:remove")
    @Log(title = "应用管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iSysApplicationService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 获取选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<OptionVo>> optionSelect(String query, String ids, String field, PageQuery pageQuery) {
        String[] queryIds = {};
        if (StringUtils.isNotBlank(ids)) {
            queryIds = ids.split(",");
        }
        List<OptionVo> optionVos = iSysApplicationService.optionSelect(query, queryIds, field, pageQuery);
        return R.ok(optionVos);
    }
}
